package com.leather.common.toolkits;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;

import java.io.IOException;

public class MessageKit {

    // 短信应用SDK AppID
    private final static int appid = 1400191674; // 1400开头

    // 短信应用SDK AppKey
    private final static String appkey = "748d9e0a0f1519e01a35f5be6be8409c";


    // 短信模板ID，需要在短信应用中申请
    private final static int templateId = 307176;

    // 签名
    private final static String smsSign = "KEVINPROJECT";


    public static boolean sendMessage(String phone, String code) {
        try {
            String[] params = {code};//数组具体的元素个数和模板中变量个数必须一致，例如事例中templateId:5678对应一个变量，参数数组中元素个数也必须是一个
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            System.out.println(result);
            return true;
        } catch (HTTPException | IOException | JSONException e) {
            // HTTP响应码错误
            e.printStackTrace();
            return false;
        }

    }

    public static boolean sendExpressMessage(String phone, String []getAddress, int tmpId) {
        try {
            String[] params = getAddress;//数组具体的元素个数和模板中变量个数必须一致，例如事例中templateId:5678对应一个变量，参数数组中元素个数也必须是一个
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    tmpId, params, smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            System.out.println(result);
            return true;
        } catch (HTTPException | IOException | JSONException e) {
            // HTTP响应码错误
            e.printStackTrace();
            return false;
        }
    }
}
